/**
 * 
 */
package com.elite.groovy.callback
import com.elite.groovy.util.EliteBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.elite.groovy.util.Util;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import cn.com.elite.PropertiesUtil;

public class VIPsync implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(VIPsync.class);
	private String charset = "utf-8"; //设置字符集
	private int timeout = 100000; //设置请求超时时间
	private int enableSSLflag = 0;	
	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response){
		
		//获取入参
		def admin=request.getParameter("admin");
		def pwd=request.getParameter("pwd");
		def time=request.getParameter("time");
		PropertiesUtil properties = new PropertiesUtil();
		def vipurl = properties.readValue("vipurl"); //获取配置文件接口地址
		
		//参数校验
		JSONObject resulterror = new JSONObject();
		if(admin.equals("") || admin==null){
			resulterror.put("code", "-1");
			resulterror.put("msg", "账号不能为空！");
			resulterror.put("data", "");			
			return  resulterror;
		}else if(pwd.equals("") || pwd== null){
			resulterror.put("code", "-1");		
			resulterror.put("msg", "密码不能为空");
			resulterror.put("data", "");
			return  resulterror;
		}else if(time.equals("") || time== null){
			resulterror.put("code", "-1");		
			resulterror.put("msg", "时间不能为空");
			resulterror.put("data", "");
			return  resulterror;
		}
		
		
		JSONObject josnbody = new JSONObject();
			josnbody.put("admin",admin);
			josnbody.put("password",pwd);
		Map headMap=new HashMap(); //设置头文件
		headMap.put("Content-Type","application/json");
		try{
			def msgjson = Util.doHttpPostUseHead(vipurl,JSONObject.fromObject(josnbody).toString(),charset,timeout,enableSSLflag,headMap);
			log.debug("----vip数据--------");
			log.debug(msgjson);
			JSONObject obj = JSONObject.fromObject(msgjson);
			def code=obj.get("code");
			if(code!= 1000){
				resulterror.put("code", "-1");		
				resulterror.put("msg", "查询异常或无返回结果");
				resulterror.put("data", "");
				return  resulterror;
			}
			//obj=obj.get("list");
			//log.debug("obj:"+obj);
			//JSONArray viplist = obj.getJSONArray("result");
			JSONArray viplist = obj.getJSONArray("list");
			log.debug("viplist:"+viplist);
			
			if(viplist.size()==0){
				resulterror.put("code", "-1");		
				resulterror.put("msg", "无返回结果");
				resulterror.put("data", "");
				return  resulterror;
			}
			
				for(def i=0;i<viplist.size();i++){
					JSONObject it = viplist.get(i);
					if(it.get("lable")=="D"){
						eliteBean.update("update coreuser.ECIF_CUSTOMER_VIP set LABLE='D' where mobile='"+it.get("mobile")+"'","dataSource");
					}else{
					
						eliteBean.update("INSERT INTO coreuser.ECIF_CUSTOMER_VIP(CUST_ID,CUST_NAME,MOBILE,COMPANY,DEPT,LABLE,UPDATE_TIME)VALUES('"+it.get("clientNo")+"','"+it.get("clientName")+"','"+it.get("mobile")+"','"+it.get("company")+"','"+it.get("dept")+"','"+it.get("lable")+"',sysdate)" ,"dataSource");
			
					}
				}			
				resulterror.put("code", "1");		
				resulterror.put("msg", "同步成功");
				resulterror.put("data", "");
				return  resulterror;
			
			
		}catch(Exception e){
			log.debug(e);
			resulterror.put("code", "-1");		
			resulterror.put("msg", "出现错误");
			resulterror.put("data", "");
			return  resulterror;
		}
		
		

 
	}
	}	
